package com.lianrui.app.goods.controller;

import static com.lianrui.message.ISystemMessage.SYS_CONTROLLER_FAILED;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianrui.app.goods.domain.Product;
import com.lianrui.app.goods.service.IProductSerivce;
import com.lianrui.app.goods.vos.ProductPage;
import com.lianrui.app.goods.vos.SpecVo;
import com.lianrui.commons.ResultView;
import com.lianrui.commons.exception.SystemException;
import com.lianrui.core.AbstractController;
import com.lianrui.core.IService;

@Controller
@RequestMapping("/product")
public class ProductController
        extends AbstractController<ProductPage, Product, Integer> {

    private static final Logger LOGGER = LoggerFactory
            .getLogger(ProductController.class);

    @Autowired
    IProductSerivce productSerivce;

    @Override
    public IService<ProductPage, Product, Integer> getService() {
        return productSerivce;
    }

    @Override
    public Logger getLogger() {
        return LOGGER;
    }

    @RequestMapping("/getProductBygoodsId/{id}")
    @ResponseBody
    public ProductPage getProductBygoodsId(@PathVariable Integer id) {
        ProductPage view = new ProductPage();
        List<Product> products = new ArrayList<Product>();
        try {
            products = this.productSerivce.getProductBygoodsId(id);
            view.setTotal(products.size());
            view.setResult(products);
        } catch (Exception e) {
            view.setCode(HttpStatus.EXPECTATION_FAILED.value());
            throw new SystemException(SYS_CONTROLLER_FAILED, getLogger(), e);
        }
        return view;
    }

    @RequestMapping("/getSpecs/{goodsId}")
    @ResponseBody
    public ResultView<List<SpecVo>> getSpecs(@PathVariable Integer goodsId) {

        ResultView<List<SpecVo>> view = new ResultView<List<SpecVo>>();
        try {

            List<SpecVo> spects = this.productSerivce.getSpecs(goodsId);
            view.setResult(spects);
        } catch (Exception e) {
            view.setCode(HttpStatus.EXPECTATION_FAILED.value());
            view.setMessage(e.getMessage());
        }

        return view;
    }

}
